package mall.controller.admin;

import mall.core.jdbc.Page;
import mall.core.web.Controller;
import mall.entity.User;
import mall.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author xf07b1
 * @wechat xf07b1
 * @description
 */
@WebServlet("/admin/user")
public class AdminUserController extends Controller {

    private final UserService userService = new UserService();

    @Override
    protected User getParameterForEntity(HttpServletRequest request) {
        User user = new User();

        String id = request.getParameter("id");
        if (id != null && !id.isEmpty()) {
            user.setId(Integer.parseInt(id));
        }

        String username = request.getParameter("username");
        if (username != null && !username.isEmpty()) {
            user.setUsername(username);
        }

        String password = request.getParameter("password");
        if (password != null && !password.isEmpty()) {
            user.setPassword(password);
        }

        String name = request.getParameter("name");
        if (name != null && !name.isEmpty()) {
            user.setName(name);
        }

        String role = request.getParameter("role");
        if (role != null && !role.isEmpty()) {
            user.setRole(Integer.valueOf(role));
        }

        String address = request.getParameter("address");
        if (address != null && !address.isEmpty()) {
            user.setAddress(address);
        }

        String phone = request.getParameter("phone");
        if (phone != null && !phone.isEmpty()) {
            user.setPhone(phone);
        }

        return user;
    }

    /**
     * 新增
     */
    public void create(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        User user = getParameterForEntity(request);
        userService.add(user);

        redirect("/admin/user?method=viewList", request, response);
    }

    /**
     * 删除
     */
    public void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = getParameterForEntity(request);
        userService.deleteById(user.getId());
        redirect("/admin/user?method=viewList", request, response);
    }

    /**
     * 修改
     */
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = getParameterForEntity(request);
        userService.update(user);

        redirect("/admin/user?method=viewList", request, response);
    }

    /**
     * 查询
     */
    public void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = getParameterForEntity(request);
        Page<User> page = getParameterForPage(request);
        userService.page(page, user);

        request.setAttribute("id", user.getId());
        request.setAttribute("name", user.getName());
        setPageAttribute(request, page);

        forward("/admin/user/list", request, response);
    }

    /**
     * 访问列表页面
     */
    public void viewList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = getParameterForEntity(request);
        Page<User> page = getParameterForPage(request);
        userService.page(page, user);

        setPageAttribute(request, page);

        forward("/admin/user/list", request, response);
    }

    /**
     * 访问新增页面
     */
    public void viewCreate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        forward("/admin/user/create", request, response);
    }

    /**
     * 访问编辑页面
     */
    public void viewEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = getParameterForEntity(request);
        user = userService.getById(user.getId());
        request.setAttribute("user", user);
        forward("/admin/user/edit", request, response);
    }
}
